package me.jessyan.mvparms.demo.mvp.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.graphics.ColorUtils;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.platform.ai.control.R;



import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.di.component.DaggerUserComponent;
import me.jessyan.mvparms.demo.di.module.UserModule;
import me.jessyan.mvparms.demo.mvp.contract.UserContract;
import me.jessyan.mvparms.demo.mvp.model.entity.MessageEvent;
import me.jessyan.mvparms.demo.mvp.model.entity.User;
import me.jessyan.mvparms.demo.mvp.model.logic.TextUtil;
import me.jessyan.mvparms.demo.mvp.model.logic.api.OkHttpUtils;
import me.jessyan.mvparms.demo.mvp.presenter.UserPresenter;
import me.jessyan.mvparms.demo.util.SpeechUtils;


@SuppressLint("ValidFragment")
public class TVFragment extends BaseFragment<UserPresenter> implements UserContract.View,SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.iv_switch)
    ImageView mSwitch;

    @BindView(R.id.iv_volume)
    ImageView mVolume;

    boolean isSwitch = false;
    boolean isVolume = false;
    public static TVFragment newInstance() {
        TVFragment fragment = new TVFragment();
        return fragment;
    }

    public TVFragment(){

    }

    @Override
    public void onResume() {
        super.onResume();
        if(getUserVisibleHint()){
            requestData();
        }
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerUserComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .userModule(new UserModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tv, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        initRecycleView();
        requestData();
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().getDispatcher();
    }




    public void requestData() {

    }

    private void initRecycleView() {
        if(isSwitch){
            mSwitch.setImageResource(R.mipmap.switch_on);
        }else{
            mSwitch.setImageResource(R.mipmap.switch_off);
        }
        if(isVolume){
            mVolume.setImageResource(R.mipmap.icon_tv_volume);
        }else{
            mVolume.setImageResource(R.mipmap.icon_tv_unvolume);
        }


        mSwitch.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {
                if(isSwitch){
                    isSwitch = false;
                    mSwitch.setImageResource(R.mipmap.switch_off);
                    new OkHttpUtils().postForTV(getContext(),"power");
                } else{
                    isSwitch = true;
                    mSwitch.setImageResource(R.mipmap.switch_on);
                    new OkHttpUtils().postForTV(getContext(),"power");
                }
            }
        });
        mVolume.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View v) {
                if(isVolume){
                    isVolume = false;
                    mVolume.setImageResource(R.mipmap.icon_tv_unvolume);
                    new OkHttpUtils().postForTV(getContext(),"SetVolumeMuteRequest");
                }else{
                    isVolume = true;
                    mVolume.setImageResource(R.mipmap.icon_tv_volume);
                    new OkHttpUtils().postForTV(getContext(),"vol_up");
                }
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_menu)
    public void turnMenu(View v){
        new OkHttpUtils().postForTV(getContext(),"menu");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_up)
    public void turnUp(View v){
        new OkHttpUtils().postForTV(getContext(),"up");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_left)
    public void turnLeft(View v){
        new OkHttpUtils().postForTV(getContext(),"left");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_right)
    public void turnRight(View v){
        new OkHttpUtils().postForTV(getContext(),"right");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_down)
    public void turnDown(View v){
        new OkHttpUtils().postForTV(getContext(),"down");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_ok)
    public void turnOk(View v){
        new OkHttpUtils().postForTV(getContext(),"ok");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_tv_back)
    public void turnBack(View v){
        new OkHttpUtils().postForTV(getContext(),"back");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_home)
    public void turnHome(View v){
        new OkHttpUtils().postForTV(getContext(),"homepage");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_air_play)
    public void turnAirPlay(View v){
        new OkHttpUtils().postForTV(getContext(),"airplay");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_vod)
    public void turnVod(View v){
        new OkHttpUtils().postForTV(getContext(),"vod");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_vol_add)
    public void volAdd(View v){
        new OkHttpUtils().postForTV(getContext(),"vol_up");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_vol_sub)
    public void volSub(View v){
        new OkHttpUtils().postForTV(getContext(),"vol_down");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_tv_add)
    public void tvADD(View v){//频道加
        new OkHttpUtils().postForTV(getContext(),"DecrementTVChannelRequest");
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @OnClick(R.id.iv_tv_sub)
    public void tvSub(View v){//频道减
        new OkHttpUtils().postForTV(getContext(),"IncrementTVChannelRequest");
    }

    /**
     * 此方法是让外部调用使fragment做一些操作的,比如说外部的activity想让fragment对象执行一些方法,
     * 建议在有多个需要让外界调用的方法时,统一传Message,通过what字段,来区分不同的方法,在setData
     * 方法中就可以switch做不同的操作,这样就可以用统一的入口方法做不同的事
     * <p>
     * 使用此方法时请注意调用时fragment的生命周期,如果调用此setData方法时onCreate还没执行
     * setData里却调用了presenter的方法时,是会报空的,因为dagger注入是在onCreated方法中执行的,然后才创建的presenter
     * 如果要做一些初始化操作,可以不必让外部调setData,在initData中初始化就可以了
     *
     * @param data
     */

    @Override
    public void setData(Object data) {

    }


    @Override
    public void showLoading() {

    }


    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        if (!message.isEmpty()) {

        }
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {

    }

    @Override
    public void killMyself() {

    }


    @Override
    public void setAdapter(DefaultAdapter adapter) {

    }

    @Override
    public void loginReturn(User user) {

    }

    @Override
    public void logout() {

    }


    public void uploadResult(String string) {

    }

    @Override
    public void onRefresh() {
        requestData();
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Subscriber
    public void onMessageEvent(MessageEvent event) {
        if (TextUtil.checkContains(event.getMessage(), new String[]{"电视"})) {
            if (TextUtil.checkContains(event.getMessage(), new String[]{"打开"})) {
                isSwitch = true;
                mSwitch.setImageResource(R.mipmap.switch_on);
                new OkHttpUtils().postForTV(getContext(), "power");
                SpeechUtils.getInstance(mContext).speakText("指令发送成功");
            } else if (TextUtil.checkContains(event.getMessage(), new String[]{"关闭"})) {
                isSwitch = false;
                mSwitch.setImageResource(R.mipmap.switch_off);
                new OkHttpUtils().postForTV(getContext(), "power");
                SpeechUtils.getInstance(mContext).speakText("指令发送成功");
            }
        } else if(TextUtil.checkContains(event.getMessage(), new String[]{"音","量","加"})){
            new OkHttpUtils().postForTV(getContext(), "vol_up");
        }else if(TextUtil.checkContains(event.getMessage(), new String[]{"音","量","减"})){
            new OkHttpUtils().postForTV(getContext(), "vol_down");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(), new String[]{"频","道","加"})){
            new OkHttpUtils().postForTV(getContext(),"DecrementTVChannelRequest");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(), new String[]{"频","道","减"})){
            new OkHttpUtils().postForTV(getContext(),"IncrementTVChannelRequest");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(), new String[]{"静音"})){
            new OkHttpUtils().postForTV(getContext(),"SetVolumeMuteRequest");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(),new String[]{"菜单"})){
            new OkHttpUtils().postForTV(getContext(),"menu");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(),new String[]{"上"})){
            new OkHttpUtils().postForTV(getContext(),"up");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(),new String[]{"左"})){
            new OkHttpUtils().postForTV(getContext(),"left");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(),new String[]{"下"})){
            new OkHttpUtils().postForTV(getContext(),"down");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(),new String[]{"右"})){
            new OkHttpUtils().postForTV(getContext(),"right");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(),new String[]{"OK"})){
            new OkHttpUtils().postForTV(getContext(),"ok");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(),new String[]{"返回"})){
            new OkHttpUtils().postForTV(getContext(),"back");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(),new String[]{"主页"})){
            new OkHttpUtils().postForTV(getContext(),"homepage");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(),new String[]{"投影"})){
            new OkHttpUtils().postForTV(getContext(),"airplay");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }else if(TextUtil.checkContains(event.getMessage(),new String[]{"影视"})){
            new OkHttpUtils().postForTV(getContext(),"vod");
            SpeechUtils.getInstance(mContext).speakText("指令发送成功");
        }



    }

}
